package com.grf.es.transport;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/**
 * 查询
 * @author 郭荣锋
 */
public class Query {

	public static void main(String[] args) throws UnknownHostException {
		Query q = new Query();
		//q.matchAllQuery();
		//q.queryStringQuery(); //
		//q.termQuery(); //
		//q.wildcardQuery(); //通配符查询
		q.fuzzy(); // 模糊查询	
	}

	/**
	 * 模糊查询 
	 * @throws UnknownHostException 
	 */
	public void fuzzy() throws UnknownHostException {
		TransportClient client = getClient(); //客户端
		//查询都用prepareSearch(),fuzzyQuery(字段名, 查询的内容)
		SearchResponse searchResponse = 
				client.prepareSearch("blog3").setTypes("article")
				.setQuery(QueryBuilders.fuzzyQuery("title", "lucenc")).get();
		SearchHits hits = searchResponse.getHits(); //命中次数
		Iterator<SearchHit> iterator = hits.iterator(); //结果集
		while (iterator.hasNext()) { //循环遍历
			SearchHit searchHit = iterator.next();
			System.out.println(searchHit.getSourceAsString());
		}
		client.close(); //关闭客户端资源
	}
	
	/**
	 * 查询所有
	 * @throws UnknownHostException
	 */
	public void matchAllQuery() throws UnknownHostException {
		TransportClient client = getClient(); //客户端
		//查询都用prepareSearch()
		SearchResponse searchResponse = 
				client.prepareSearch("blog3").setTypes("article")
				.setQuery(QueryBuilders.matchAllQuery()).get();
		SearchHits hits = searchResponse.getHits(); //命中次数
		Iterator<SearchHit> iterator = hits.iterator(); //结果集
		while (iterator.hasNext()) { //循环遍历
			SearchHit searchHit = iterator.next();
			System.out.println(searchHit.getSourceAsString());
		}
		client.close(); //关闭客户端资源
	}
	
	/**
	 * 通配符查询 
	 * @throws UnknownHostException 
	 */
	public void wildcardQuery() throws UnknownHostException {
		TransportClient client = getClient(); //客户端
		//查询都用prepareSearch()
		SearchResponse searchResponse = 
				client.prepareSearch("blog3").setTypes("article")
				.setQuery(QueryBuilders.wildcardQuery("content", "?分")).get();
		SearchHits hits = searchResponse.getHits(); //命中次数
		Iterator<SearchHit> iterator = hits.iterator(); //结果集
		while (iterator.hasNext()) { //循环遍历
			SearchHit searchHit = iterator.next();
			System.out.println(searchHit.getSourceAsString());
		}
		client.close(); //关闭客户端资源
	}
	
	/**
	 * 字段分词查询(把查询的字段进行分词)
	 * @throws UnknownHostException 
	 */
	public void queryStringQuery() throws UnknownHostException {
		TransportClient client = getClient(); //客户端
		//查询都用prepareSearch()
		SearchResponse searchResponse = 
				client.prepareSearch("blog3").setTypes("article")
				.setQuery(QueryBuilders.queryStringQuery("lucence")).get();
		SearchHits hits = searchResponse.getHits(); //命中次数
		Iterator<SearchHit> iterator = hits.iterator(); //结果集
		while (iterator.hasNext()) { //循环遍历
			SearchHit searchHit = iterator.next();
			System.out.println(searchHit.getSourceAsString());
		}
		client.close(); //关闭客户端资源
	}
	
	/**
	 * 词条查询(默认的中文分词器把每个字为索引)
	 * @throws UnknownHostException 
	 */	
	public void termQuery() throws UnknownHostException {
		TransportClient client = getClient(); //客户端
		//查询都用prepareSearch()
		SearchResponse searchResponse = 
				client.prepareSearch("blog3").setTypes("article")
				.setQuery(QueryBuilders.termQuery("content", "分")).get();
		SearchHits hits = searchResponse.getHits(); //命中次数
		Iterator<SearchHit> iterator = hits.iterator(); //结果集
		while (iterator.hasNext()) { //循环遍历
			SearchHit searchHit = iterator.next();
			System.out.println(searchHit.getSourceAsString());
		}
		client.close(); //关闭客户端资源
	}
	
	/**
	 * 
	 * 创建索引
	 * @throws UnknownHostException 
	 */
	public void createIndex(String indexName) throws UnknownHostException {
		TransportClient client = getClient();
		client.admin().indices().prepareCreate("test").get();
		client.close();
	}
	
	/**
	 * 获取客户端 
	 * @throws UnknownHostException 
	 */
	public TransportClient getClient() throws UnknownHostException {
		TransportClient client  ;
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop101"),9300));
		
		return client;
	}
}
